﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace KLY.Tools.Helper
{
    public static class JsonHelper
    {
        #region Api

        public static bool Indented
        {
            get { return _indented; }
            set { _indented = value; }
        }

        public static string Serialize(object result)
        {
            return Serialize(result, _indented);
        }

        public static string Serialize(object result, bool formate)
        {
            try
            {
                var json = JsonConvert.SerializeObject(result, formate ? Formatting.Indented : Formatting.None, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                return json;
            }
            catch(Exception ex)
            {
                LogHelper.WriteLogError("Error", ex);
            }
            return "";
        }

        public static T DeSerialize<T>(string result)
        {
            try
            {
                var json = JsonConvert.DeserializeObject<T>(result, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                return json;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogError("Error", ex);
            }
            return default(T);
        }

        #endregion

        #region Private Property

        private static bool _indented = false;

        #endregion
    }
}
